<template>
  <div>

      <div style="margin-bottom:15px">
        <el-input v-model="params.tutorName" style="width:200px" placeholder="Enter the tutor name"></el-input>
        <el-input v-model="params.typeName" style="width:200px;margin-left:5px" placeholder="Enter category"></el-input>
        <el-button type="warning" style="margin-left:10px" @click="findBySearch()">query</el-button>
        <el-button type="warning" style="margin-left:10px" @click="reset()">clear</el-button>
        <el-button type="primary" style="margin-left:10px" @click="add()" v-if="user.role === 'ROLE_ADMIN'">add</el-button>
      </div>
      <div>
        <el-table :data="tableData" style="width: 100%">
          
          <el-table-column prop="tutorName" label="指导教师姓名" ></el-table-column>
          
          <el-table-column prop="studentName" label="学生姓名"></el-table-column>
          <el-table-column prop="typeName" label="研究方向"></el-table-column>


          <el-table-column prop="status" label="审核状态"></el-table-column>
          <el-table-column prop="reason" label="审核意见"></el-table-column>
          <el-table-column prop="time" label="预订时间"></el-table-column>
          
          <el-table-column  label="操作" width="250px">
            <template slot-scope="scope">
              <el-button type="success" @click="audit(scope.row)" v-if="user.role !=='ROLE_STUDENT'">审核</el-button>

              
              <el-popconfirm title="Are you sure to delete?" @confirm="del(scope.row.id)">
                <el-button slot="reference" type="danger" style="margin-left:5px" v-if="user.role === 'ROLE_ADMIN'">删除</el-button>
              </el-popconfirm>
              <!-- <el-button type="danger">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>

        <div style="margin-top: 10px;">
          
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="params.pageNum"
                  :page-sizes="[5, 10, 15, 20]"
                  :page-size="params.pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
                </el-pagination>
        </div>


        <!-- 审核信息 -->

        <div>

                    <el-dialog title="请审核" :visible.sync="auditVisible" width="55%">
            <el-form :model="form">
               
               <el-form-item label="审核状态" label-width="15%">
                <el-radio v-model="form.status" label="Pass"></el-radio>
                <el-radio v-model="form.status" label="Not Pass"></el-radio>
              </el-form-item>

              <el-form-item label="审核意见" label-width="15%">
                <el-input v-model="form.reason" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="auditVisible = false">取消</el-button>
              <el-button type="primary" @click="submit()">确定</el-button>
            </div>
          </el-dialog>

        </div>


  </div>
</template>

<script>
import request from '@/utils/request'

import E from 'wangeditor'

let editor
// function initWangEditor(content) {	setTimeout(() => {
//   if (!editor) {
//     editor = new E('#editor')
//     editor.config.placeholder = '请输入内容'
//     editor.config.uploadFileName = 'file'
//     //文件上传的接口
//     editor.config.uploadImgServer = 'http://localhost:8080/api/files/wang/upload'
//     editor.create()
//   }
//   editor.txt.html(content)
// }, 0)
// }

function initWangEditor(content) {
  setTimeout(() => {
    if (editor) {
      editor.destroy(); // 销毁旧实例
    }
    editor = new E('#editor');
    editor.config.placeholder = '请输入内容';
    editor.config.uploadFileName = 'file';
    editor.config.uploadImgServer = 'http://localhost:8080/api/files/wang/upload';
    editor.create();
    editor.txt.html(content || '');
  }, 100);
}

    export default {
      data() {
        return {
          params:{
            tutorName:'',
            typeName:'',
            pageNum:1,
            pageSize:5
          },
          tableData: [],
          total: 0,
          //dialogFormVisible: false,
          //form:{},
          dialogFormVisible: false,
          editorVisible:false,
          //dialogCommunicateVisible:false,
          viewData:'',
          typeObjs:[],
          auditVisible: false,
          form:{},
          user:localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{},
          payVisible: false,
          paymentAmount: '',
          paymentMethod: 'AliPay',
          qrCodeUrl: 'https://688522ckp.oss-cn-beijing.aliyuncs.com/2025/01/589bcb01-d34b-4875-98f7-6c14d29fe5ef.png', // 示例二维码 URL
          qrCodeUrl2:'https://688522ckp.oss-cn-beijing.aliyuncs.com/2025/01/0ef3d17d-f307-4176-bc79-0099182cd636.png',
          tutorName:''
        }
      },
      //页面加载的时候，做一些事情，在created里面
      created(){
         this.findBySearch();
         this.findTypes();
      },
      //定义一些页面上空间触发的事件调用的方法
      methods:{ 
        findBySearch(){
          request.get("/hire/search",{
            params:this.params
          }).then(res =>{
            if(res.code === '0'){
              this.tableData = res.data.list
              this.total = res.data.total
            }else{
                  this.$message.error(res.msg)
            }
          })
        },
        add(){
          this.form = {}
          this.dialogFormVisible = true
        },
        review(obj){
          this.form = obj,
          initWangEditor("")
          this.dialogFormVisible = true
          //alert(this.form.tutorId)
        },
        evaluate(){
          //alert(this.form.tutorId)
          //alert(this.form.tutorName)
          this.form.content = editor.txt.html()
          //alert(this.form.content)
          let param = {
            id:this.form.tutorId,
            content:this.form.content
          }
          

          request.put("/admin/evaluate",param).then(res =>{
            if(res.code === '0'){
                this.$message.success("Evaluation success")
                this.dialogFormVisible = false
                //this.auditVisible = false
                this.findBySearch()
            }else{
                
                this.$message.error(res.msg)
            }
          })

        },
        
        edit(obj){
          this.dialogFormVisible = true
          this.form = obj

        },
        reset(){
           this.params={
            pageNum:1,
            pageSize:5,
            name:'',
            author:''
           }
           this.findBySearch()
        },
        audit(obj){
          this.form = obj;
          this.auditVisible = true
        },
        submit(){
          request.post("/hire/audit",this.form).then(res =>{
            if(res.code === '0'){
                this.$message.success("Review successful")
                //this.dialogFormVisible = false
                this.auditVisible = false
                this.findBySearch()
            }else{
                
                this.$message.error(res.msg)
            }
          })
        },
        payment(obj){

          this.form = obj
          // alert(this.form.id)
          // alert(this.form.tutorId)

          let param = {
            id:this.form.id
          }
          
          request.get("/admin/getTutorInformation",{
               params: param
            }).then(res =>{
            if(res.code === '0'){
            
              this.paymentAmount = res.data.price
              this.tutorName = res.data.name
              this.payVisible = true;
            }else{
                  this.$message.error("Failed to obtain tutoring information")
            }
          }).catch(error => {
            // 捕获请求错误
            console.error("Request failed:", error);
            this.$message.error("Network error or server is unreachable");
          });

          
        },
        handlePayment(){
            request.put("/hire/payment",this.form).then(res =>{
              if(res.code === '0'){
                  this.$message.success("Pay Success")
                  this.payVisible = false
                  this.findBySearch()
              }else{
                  
                  this.$message.error(res.msg)
              }
            })
        },
        paymessage(){
          this.message.success("Pay Success")
        },
        handleSizeChange(pageSize){
           this.params.pageSize = pageSize
           this.findBySearch()
        },
        handleCurrentChange(pageNum){
          this.params.pageNum = pageNum
           this.findBySearch()
        },
        del(id){
          request.delete("/reserve/"+id).then(res =>{
            if(res.code === '0'){
              this.$message.success("Delete successfully")
              this.findBySearch()
            }else{
              this.$message.error(res.msg)
            }
          })
        },
  
      }
    }
  </script>